<?php
namespace app\student\controller;

use app\student\controller\PublicController;
use think\Db;
use app\student\model\AdminMenuModel;
use app\common\model\MytaskModel;
use app\common\model\RecordModel;
use app\common\model\AppUserModel;
use app\common\model\TemplateGroupModel;
use app\common\logic\home\Mytask;

//我的课题
class MytaskController extends PublicController
{
    public function __construct(){
        parent::__construct();
        $this->check();
    }

    /**
     * 我的课题列表页
     */
    public function index()
    {
        $param = $this->request->param();
        $data = MytaskModel::where('uid',$this->user_id)->order('id desc')->paginate(20,false,['query'=>$param]);
        $this->assign('page',$data->render());

        $datas = $data->toArray()['data'];
        
        foreach($data as $k => $val){
            $datas[$k]['teacher_info']['realName'] = null;
            $datas[$k]['teacher_info']['header'] = null;
            $t_user_id =  RecordModel::where('mytask_id',$val['id'])->value('t_user_id');
            if(!empty($t_user_id)){
                $rs = AppUserModel::where('id',$t_user_id)->field('realName,header')->find();
                if(!empty($rs)){
                     $datas[$k]['teacher_info'] = $rs->toArray();
                }
            }  
            $datas[$k]['create_time'] = date('Y-m-d',strtotime($val['create_time']));
                //dump($val);       
                 
         
        }

        $this->assign('datas',$datas);
              
        return $this->fetch('student@mystask/index');
    }
	/**
     * [课题详情]
     * @param  Mytask $Mytask [description]
     * @return [type]         [description]
     */
    public function info(Mytask $Mytask){
        $param = $this->request->param();
        $data = $Mytask->index($param['mytask_id'],$param['user_id']);
        $data_template = TemplateGroupModel::get($param['group_id']);
        $this->assign('data_template',$data_template);
        $this->assign('data',$data);
        return $this->fetch('student@:mystask/info');
    }

    public function export(Mytask $Mytask){

        $param = $this->request->param();
        $data = $Mytask->index($param['mytask_id'],$param['user_id']);
        $data_template = TemplateGroupModel::get($param['group_id']);
        $this->assign('data_template',$data_template);
        $this->assign('data',$data);
        $a=$this->fetch('student@:mystask/info1');
   
        //return $this->fetch();
        header("Content-Type:   application/msword");
        header("Content-Disposition:   attachment;   filename=agreement.doc"); //指定文件名称
        header("Pragma:   no-cache");
        header("Expires:   0");
         
        echo  $a;
    }
}
